<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29 0029
 * Time: 上午 9:12
 */

namespace app\admin\model;

use app\admin\model\Base;
use think\Db;

class Role extends Base
{

    protected $table = "authority_role";

    //基本数据表
    protected $base_arr = [
        'table' => 'authority_role',
        'field' => 'rolename,state,comment',
        'primary_key' => 'id'
        ];

    //附属的表单
    protected $attach_arr =  [
        [
        'table' => 'authority_role',
        'field' => 'rolename',
        'primary_key' => 'id',
        'base_table_field' => 'uproleid'
        ],

        ];

    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql(){
        $data = [
            't_name' => 'authority_role',
            'model' => '角色',
            'rolename' => '角色名称',
           // 'uproleid' => '上级角色',
            'comment' => '描述',
            'state' => '{"0":"功能禁用","1":"功能启用","2":"功能删除"}'
        ];
        return $data;
    }

    /**
     * 管理数据
    **/
    public static function addRoleAdminLog($list_action,$data=[],$userId,$admin_type){


        $model = new Role();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];

        switch($actionName){
            case "rolelist":
                Base::save_admin_log($userId,$model,'check');
                break;
            case "addrole":
                $type = 'add';

                if ($model->request->isPost()) {

                    if ($data["uproleid"] == 0) {
                        // 上级组织为0时表示新增的是根目录
                        $data["uproleids"] = "0,";
                        $data["level"] = 1;
                        // 获取排序阶层
                        $sqlsort = "select * from authority_role where `sortlevel` like '%,' and (length(`sortlevel`) - length(replace(sortlevel,',',''))) = 1 order by sortlevel desc";
                        $listsort = Db::query($sqlsort);
                        if ($listsort == null) {
                            // 没有子阶层的时候从0开始
                            $data["sortlevel"] = "0,";
                        } else {
                            // 有子阶层取最后一个阶层的最后一位数字+1
                            $temp = explode(",", $listsort[0]["sortlevel"]);
                            $data["sortlevel"] = ($temp[0] + 1) . ",";
                        }
                        //print_r($data);

                    } else {
                        // 新增子目录功能
                        $list = Db::query("select * from authority_role where id=?", [$data["uproleid"]]);
                        if ($list == null) {
                            return ['flag' => 'error', 'msg' => '未查找到上一级', 'url' => ''];
                        } else {
                            $data["uproleids"] = $list[0]["uproleids"] . $data["uproleid"] . ",";
                            $data["level"] = $list[0]["level"] + 1;

                            // 获取排序阶层
                            $sqlsort = "select * from authority_role where `sortlevel` like ? and (length(`sortlevel`) - length(replace(sortlevel,',',''))) = ? order by sortlevel desc";
                            $listsort = Db::query($sqlsort, [$list[0]["sortlevel"] . "%,", $data["level"]]);
                            if ($listsort == null) {
                                // 没有子阶层的时候从0开始
                                $data["sortlevel"] = $list[0]["sortlevel"] . "0,";
                            } else {
                                // 有子阶层取最后一个阶层的最后一位数字+1
                                $temp = explode(",", $listsort[0]["sortlevel"]);
                                $data["sortlevel"] = $list[0]["sortlevel"] . ($temp[count($temp) - 2] + 1) . ",";
                            }
                        }

                    }


                }

                break;
            case "editrole":
                $type = 'edit';

                if ($model->request->isPost()) {

                    if($data['uproleid'] == 0) {
                        // 有子阶层取最后一个阶层的最后一位数字+1
                        $data["sortlevel"] =  "0,";
                        $data["uproleids"] =  "0,";

                    }else{
                        $list = Db::query("select * from authority_role where id=?", [$data["uproleid"]]);
                        // 有子阶层取最后一个阶层的最后一位数字+1
                        $temp = explode(",", $list[0]["sortlevel"]);
                        $data["sortlevel"] = ($temp[0] + 1) . ",";
                        $data["uproleids"] = $list[0]["uproleids"] . $data["uproleid"] . ",";
                    }

                    $old_data = Base::getOneOldDataByTables($data['id'],$model->base_arr,$model->attach_arr);

                }

                break;
            case "deleterole":
                $type = 'del';
                if ($model->request->isPost()) {

                    $state = 2;
                    Db::execute("update authority_role set state=? where id=?", [$state, $data['id']]);


                }
                break;

        }



            if($model->request->isPost()){
                $result =  Base::operaTable($model,$userId,$type,$data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type);
                return $result;
            }


        return true;
    }



}